﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using Movies.CopyUtility;

namespace Movies
{
    public partial class FormMainWindow : Form
    {
        
        public FormMainWindow()
        {
            InitializeComponent();
        }



        private void defineMovieFolderMenu_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowMovieDefineMovieWin();
        }

        private void showMyMovies_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ApplyChangesOnLoadedWin();
        }
 
        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }


        private void MainWindow_Load(object sender, EventArgs e)
        {
            HelperSettingProvider.SettingLocation(Properties.Settings.Default.dataStoreLocation + "\\AppData");

            //MessageBox.Show(Properties.Settings.Default.dataStoreLocation);
            if (!HelperSettingProvider.Default().NickName.Trim().Equals(""))
            {
                this.Text = HelperSettingProvider.Default().NickName + "'s Movie Manager [MyMo " + this.Text + "]";
            }
            else if (!HelperSettingProvider.Default().FirstName.Trim().Equals(""))
            {
                this.Text = HelperSettingProvider.Default().FirstName + "'s Movie Manager [MyMo " + this.Text + "]";
            }
            else
            {
                this.Text = "MyMo Manager [" + this.Text + "]";
            }


            WinDisplayControllor.MDIParent = this;
            HelperDataProvider.getDataStore().initDataStore();
            if (HelperSettingProvider.Default().DefaultDisplay.Trim().Equals("0"))
            {
                detailedDisplayMenuItem.Checked = false;
                iconDisplayToolStripMenuItem.Checked = true;
            }
            else
            {
                iconDisplayToolStripMenuItem.Checked = false;
                detailedDisplayMenuItem.Checked = true;
            }
            updateIconSizeInfo();
            WinDisplayControllor.ShowQuickStart(false);
            WinDisplayControllor.ShowDefaultMovieWin();

        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripIconSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void reloadMovieDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HelperDataProvider.getDataStore().scanMoviesDetailsChanges();
            WinDisplayControllor.ApplyChangesOnLoadedWin();
        }

        private void iconDisplayToolStripMenuItem_Click(object sender, EventArgs e)
        {

            HelperSettingProvider.Default().DefaultDisplay = "0";
            HelperSettingProvider.Default().Save();
                detailedDisplayMenuItem.Checked = false;
                iconDisplayToolStripMenuItem.Checked = true;
                WinDisplayControllor.ShowMovieShelve();
        }

        private void detailedDisplayMenuItem_Click(object sender, EventArgs e)
        {

            HelperSettingProvider.Default().DefaultDisplay = "1";
            HelperSettingProvider.Default().Save();
                iconDisplayToolStripMenuItem.Checked = false;
                detailedDisplayMenuItem.Checked = true;
                WinDisplayControllor.ShowMovieList();
        }

        private void sizeIconToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem currentItem = (ToolStripMenuItem)sender;
            int index = Convert.ToInt32(currentItem.Tag);
            smallToolStripMenuItem.Checked = false;
            normalIconToolStripMenuItem.Checked = false;
            mediumIconToolStripMenuItem.Checked = false;
            largeIconToolStripMenuItem.Checked = false;
            largerIconToolStripMenuItem.Checked = false;
            HelperSettingProvider.Default().ButtonSize = ""+index;
            HelperSettingProvider.Default().Save();
            currentItem.Checked = true;
            WinDisplayControllor.ShowMovieShelve();
        }

        private void updateIconSizeInfo()
        {
            smallToolStripMenuItem.Checked = false;
            normalIconToolStripMenuItem.Checked = false;
            mediumIconToolStripMenuItem.Checked = false;
            largeIconToolStripMenuItem.Checked = false;
            largerIconToolStripMenuItem.Checked = false;
            int buttonSize = 2;

            try
            {
                buttonSize = Int16.Parse(HelperSettingProvider.Default().ButtonSize);
            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
            }

            switch (buttonSize)
            {
                case 0:
                    smallToolStripMenuItem.Checked = true;
                    break;
                case 1:
                    normalIconToolStripMenuItem.Checked = true;
                    break;
                case 3:
                    largeIconToolStripMenuItem.Checked = true;
                    break;
                case 4:
                    largerIconToolStripMenuItem.Checked = true;
                    break;
                default:
                    mediumIconToolStripMenuItem.Checked = true;
                    break;
            }
        }

        private void defaultSaveLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowDataStoreWin();
        }

        private void downloadMovieDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowDetailsDownloadWin();
        }

        private void bgProcessProgress_Click(object sender, EventArgs e)
        {
            HeplerBGProcesses.showProgressDetails();
        }

        
        delegate void UpdateProcessInfo(int perc);
        public void updateProcessInfo(int percent)
        {
            if (statusStrip.InvokeRequired)
            {
                UpdateProcessInfo d = new UpdateProcessInfo(updateProcessInfo);
                this.Invoke(d, new object[] { percent });
            }else{
                if (percent < 100 && percent > 0)
                {
                    bgProcessProgress.Value = percent;
                    bgProcessProgress.Visible = true;
                    bgProcessLabel.Visible = true;
                }
                else
                {
                    bgProcessProgress.Visible = false;
                    bgProcessLabel.Visible = false;
                }
            }
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bgProcessProgress.Visible)
            {
                e.Cancel = true;
                WinDisplayControllor.DisplayDialog("Can't close before Background Process finishes their Job(s)");
                HeplerBGProcesses.showProgressDetails();

            }
        }

        private void commentsOrReportBugToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("http://code.google.com/p/vademaa-mymo/issues/entry");
        }

        private void changeNameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowChangeName();
        }

        private void quickStartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            WinDisplayControllor.ShowQuickStart(true);
        }


    }
}
